#add circle and origin point for fisheye img

from PIL import Image, ImageDraw
import os
import sys

def add_fisheye_effects(image_path, output_suffix="_"):
    """
    为鱼眼照片添加青色轮廓和圆心标记
    
    参数:
    image_path: 输入图像路径
    output_suffix: 输出文件名的前缀添加字符
    """
    try:
        # 打开原始图像
        original_image = Image.open(image_path)
        
        # 创建一个可绘制的图像副本
        image_with_effects = original_image.copy()
        draw = ImageDraw.Draw(image_with_effects)
        
        # 获取图像尺寸
        width, height = original_image.size
        
        # 计算鱼眼轮廓的圆心和半径
        # 圆心位于图像中心
        center_x, center_y = width // 2+12, height // 2+10
        
        # 轮廓半径为图像短边的90%，确保轮廓在图像内
        radius = int(min(width, height) * 0.5)
        
        # 定义青色 (R, G, B)
        cyan_color = (0, 255, 255)  # 纯青色
        
        # 1. 绘制鱼眼轮廓（圆形）
        # 轮廓边界框：左上角(center_x-radius, center_y-radius)到右下角(center_x+radius, center_y+radius)
        outline_bbox = [
            center_x - radius,  # 左
            center_y - radius,  # 上
            center_x + radius,  # 右
            center_y + radius   # 下
        ]
        
        # 绘制圆形轮廓，宽度为3像素
        draw.ellipse(outline_bbox, outline=cyan_color, width=3)
        
        # 2. 添加圆心标记（十字线）
        cross_size = 15  # 十字线大小
        
        # 绘制水平线
        draw.line([
            (center_x - cross_size, center_y),  # 左端点
            (center_x + cross_size, center_y)   # 右端点
        ], fill=cyan_color, width=2)
        
        # 绘制垂直线
        draw.line([
            (center_x, center_y - cross_size),  # 上端点
            (center_x, center_y + cross_size)   # 下端点
        ], fill=cyan_color, width=2)
        
        # 3. 保存处理后的图像
        # 获取原文件路径信息
        file_dir = os.path.dirname(image_path)
        file_name = os.path.basename(image_path)
        name_part, ext_part = os.path.splitext(file_name)
        
        # 构建新文件名：在原文件名前添加指定后缀
        new_file_name = f"{output_suffix}{name_part}{ext_part}"
        new_file_path = os.path.join(file_dir, new_file_name)
        
        # 保存图像（保持原格式）
        image_with_effects.save(new_file_path)
        print(f"处理成功！图像已保存为: {new_file_path}")
        
        return new_file_path
        
    except Exception as e:
        print(f"处理图像时出错: {e}")
        return None

def main():
    """主函数：处理单张图像"""
    if len(sys.argv) != 2:
        print("使用方法: python fisheye_processor.py <图片路径>")
        print("示例: python fisheye_processor.py my_photo.jpg")
        return
    
    image_path = sys.argv[1]
    
    # 检查文件是否存在
    if not os.path.exists(image_path):
        print(f"错误：文件 '{image_path}' 不存在")
        return
    
    # 处理图像
    add_fisheye_effects(image_path, "_")

if __name__ == "__main__":
    main()
